<template>
  <div class="sect-detail-page">
    <!-- 页面标题 -->
    <div class="page-header">
      <button class="back-btn" @click="goBack">← 返回</button>
      <h1 class="page-title">{{ safeSectInfo.name || '门派详情' }}</h1>
    </div>

    <!-- 门派信息容器 -->
    <div class="sect-container" v-if="!isLoading">
      <!-- 门派基本信息 -->
      <div class="info-card basic-info">
        <div class="card-title">门派总览</div>
        <div class="info-grid">
          <div class="info-item">
            <span class="info-label">{{ getLeaderTitle(safeSectInfo.name) }}</span>
            <span class="info-value">{{ safeSectInfo.leader || '未知' }}</span>
          </div>
          <div class="info-item">
            <span class="info-label">门派弟子</span>
            <span class="info-value">{{ safeSectInfo.memberCount || 0 }}/{{ safeSectInfo.maxMembers || 100 }}</span>
          </div>
          <div class="info-item">
            <span class="info-label">门派等级</span>
            <span class="info-value">{{ safeSectInfo.level || 1 }}级</span>
          </div>
          <div class="info-item">
            <span class="info-label">门派声望</span>
            <span class="info-value">{{ safeSectInfo.reputation || 0 }}</span>
          </div>
        </div>
      </div>

      <!-- 门派描述 -->
      <div class="info-card description-card" v-if="safeSectInfo.description">
        <div class="card-title">门派背景</div>
        <div class="description-content">
          {{ safeSectInfo.description }}
        </div>
      </div>

      <!-- 我的门派信息 -->
      <div class="info-card my-info">
        <div class="card-title">我的信息</div>
        <div class="info-grid">
          <div class="info-item">
            <span class="info-label">门派职位</span>
            <span class="info-value highlight">{{ safeSectInfo.rank || '普通弟子' }}</span>
          </div>
          <div class="info-item">
            <span class="info-label">门派贡献</span>
            <span class="info-value highlight">{{ safeSectInfo.contribution || 0 }}</span>
          </div>
        </div>
      </div>

      <!-- 门派功能入口 -->
      <div class="info-card functions-card">
        <div class="card-title">门派功能</div>
        <div class="functions-grid">
          <div class="function-item" @click="goToSectSkills">
            <div class="function-icon">📚</div>
            <div class="function-name">门派武学</div>
          </div>
          <div class="function-item" @click="goToSectMembers">
            <div class="function-icon">👥</div>
            <div class="function-name">门派成员</div>
          </div>
          <div class="function-item" @click="goToSectTasks">
            <div class="function-icon">📋</div>
            <div class="function-name">门派任务</div>
          </div>
          <div class="function-item" @click="goToSectShop">
            <div class="function-icon">🏪</div>
            <div class="function-name">门派商店</div>
          </div>
        </div>
      </div>

      <!-- 门派公告 -->
      <div class="info-card notice-card" v-if="safeSectInfo.notice">
        <div class="card-title">门派公告</div>
        <div class="notice-content">
          <div class="notice-time" v-if="safeSectInfo.noticeTime">
            {{ formatTime(safeSectInfo.noticeTime) }}
          </div>
          <div class="notice-text">{{ safeSectInfo.notice }}</div>
        </div>
      </div>

      <!-- 刷新按钮 -->
      <div class="refresh-area">
        <button @click="refreshSectInfo" class="refresh-btn" :disabled="isLoading">
          {{ isLoading ? '刷新中...' : '🔄 刷新信息' }}
        </button>
      </div>
    </div>

    <!-- 加载状态 -->
    <div v-if="isLoading" class="loading-container">
      <div class="loading-spinner"></div>
      <div class="loading-text">加载门派信息中...</div>
    </div>
    
    <!-- 全局底部导航栏 -->
    <CustomTabBar />
  </div>
</template>

<script>
import { mapState, mapActions, mapGetters } from 'vuex'

export default {
  name: 'SectDetailPage',
  data() {
    return {
      currentTime: '',
      refreshTimer: null
    }
  },
  computed: {
    ...mapGetters('sect', ['hasJoinedSect', 'sectId', 'sectName', 'isLoading']),
    ...mapState('sect', ['sectInfo']),
    
    // 确保 sectInfo 始终有默认值
    safeSectInfo() {
      return this.sectInfo || {}
    }
  },
  mounted() {
    this.loadSectInfo()
    this.updateTime()
    
    this.refreshTimer = setInterval(() => {
      this.updateTime()
    }, 30000)
  },
  beforeDestroy() {
    if (this.refreshTimer) {
      clearInterval(this.refreshTimer)
    }
  },
  methods: {
    ...mapActions('sect', ['fetchSectInfo']),
    
    async loadSectInfo() {
      try {
        await this.fetchSectInfo()
        
        if (!this.sectInfo || this.sectInfo.notJoined) {
          uni.showToast({
            title: '您还未加入任何门派',
            icon: 'none',
            duration: 2000
          })
          setTimeout(() => {
            uni.navigateTo({
              url: '/pages/sect/select'
            })
          }, 2000)
          return
        }
      } catch (error) {
        console.error('加载门派信息失败:', error)
        uni.showToast({
          title: error.message || '加载门派信息失败',
          icon: 'none'
        })
      }
    },
    
    async refreshSectInfo() {
      await this.loadSectInfo()
      uni.showToast({
        title: '信息已刷新',
        icon: 'success'
      })
    },
    
    updateTime() {
      this.currentTime = new Date().toLocaleString()
    },
    
    formatTime(timestamp) {
      if (!timestamp) return ''
      const date = new Date(timestamp)
      return date.toLocaleString()
    },
    
    getLeaderTitle(sectName) {
      const titleMap = {
        '桃花岛': '岛主',
        '白驼山庄': '庄主',
        '大理段氏': '王爷',
        '丐帮': '帮主',
        '全真教': '掌教'
      }
      return titleMap[sectName] || '掌门'
    },
    
    goToSectSkills() {
      uni.navigateTo({
        url: `/pages/sect/skills?sectId=${this.sectId}`
      })
    },
    
    goToSectMembers() {
      uni.showToast({
        title: '门派成员功能开发中',
        icon: 'none'
      })
    },
    
    goToSectTasks() {
      uni.showToast({
        title: '门派任务功能开发中',
        icon: 'none'
      })
    },
    
    goToSectShop() {
      uni.showToast({
        title: '门派商店功能开发中',
        icon: 'none'
      })
    },
    
    goBack() {
      // 直接返回首页，避免返回到门派选择页面又被重定向回来
      if (typeof uni !== 'undefined' && uni.reLaunch) {
        uni.reLaunch({
          url: '/pages/main/index'
        })
      } else {
        window.location.hash = '#/pages/main/index'
      }
    }
  }
}
</script>

<style scoped>
/* 门派详情页面 - 武侠风格 */
.sect-detail-page {
  min-height: 100vh;
  max-height: 100vh;
  overflow: hidden;
  background: linear-gradient(180deg, #e8dcc4 0%, #d4c4a8 100%);
  display: flex;
  flex-direction: column;
  font-family: "STSong", "Noto Serif SC", serif;
  padding-bottom: 70px;
}

/* 页面标题 */
.page-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 15px;
  background: linear-gradient(135deg, #8b6f47 0%, #6b4f3b 100%);
  border-bottom: 3px solid #4b3a2a;
  box-shadow: 0 4px 12px rgba(139, 111, 71, 0.3);
  flex-shrink: 0;
}

.back-btn {
  padding: 6px 12px;
  background: rgba(255, 255, 255, 0.2);
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 8px;
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
}

.back-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  border-color: rgba(255, 255, 255, 0.5);
}

.page-title {
  flex: 1;
  font-size: 20px;
  font-weight: bold;
  color: #fff;
  letter-spacing: 2px;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
  text-align: center;
}

/* 门派信息容器 */
.sect-container {
  flex: 1;
  overflow-y: auto;
  padding: 12px 15px 15px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* 信息卡片 */
.info-card {
  background: rgba(255, 255, 255, 0.95);
  border: 2px solid #c4a57b;
  border-radius: 12px;
  padding: 15px;
  box-shadow: 0 3px 10px rgba(139, 111, 71, 0.2);
}

.card-title {
  font-size: 16px;
  font-weight: bold;
  color: #4a3c2a;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 2px solid #d8c7a3;
  letter-spacing: 1px;
}

/* 信息网格 */
.info-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.info-item {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 10px;
  background: rgba(248, 246, 241, 0.6);
  border-radius: 8px;
  border: 1px solid #d8c7a3;
}

.info-label {
  font-size: 12px;
  color: #8b6f47;
  font-weight: 600;
}

.info-value {
  font-size: 15px;
  color: #4a3c2a;
  font-weight: bold;
}

.info-value.highlight {
  color: #8b6f47;
  font-size: 16px;
}

/* 描述卡片 */
.description-content {
  font-size: 14px;
  color: #6b4f3b;
  line-height: 1.8;
  text-align: justify;
  text-indent: 2em;
  padding: 12px;
  background: rgba(139, 111, 71, 0.05);
  border-radius: 8px;
  border-left: 3px solid #8b6f47;
}

/* 功能网格 */
.functions-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.function-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 15px 10px;
  background: rgba(248, 246, 241, 0.6);
  border: 2px solid #d8c7a3;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.function-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(139, 111, 71, 0.3);
  border-color: #8b6f47;
  background: rgba(248, 246, 241, 0.9);
}

.function-icon {
  font-size: 28px;
}

.function-name {
  font-size: 13px;
  font-weight: 600;
  color: #4a3c2a;
  letter-spacing: 0.5px;
}

/* 公告卡片 */
.notice-content {
  background: rgba(248, 246, 241, 0.6);
  border-radius: 8px;
  padding: 12px;
  border-left: 3px solid #8b6f47;
}

.notice-time {
  font-size: 11px;
  color: #8b6f47;
  margin-bottom: 8px;
}

.notice-text {
  font-size: 13px;
  color: #6b4f3b;
  line-height: 1.6;
}

/* 刷新区域 */
.refresh-area {
  display: flex;
  justify-content: center;
  padding-top: 5px;
}

.refresh-btn {
  width: 100%;
  padding: 12px;
  background: linear-gradient(135deg, #8b6f47 0%, #6b4f3b 100%);
  color: white;
  border: 2px solid #4b3a2a;
  border-radius: 10px;
  font-size: 14px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s ease;
  letter-spacing: 1px;
  box-shadow: 0 3px 10px rgba(75, 58, 42, 0.25);
}

.refresh-btn:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 5px 15px rgba(75, 58, 42, 0.35);
}

.refresh-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  background: #d0d0d0;
  border-color: #999;
  color: #666;
}

/* 加载状态 */
.loading-container {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 15px;
  padding: 30px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  border: 2px solid #8b6f47;
  box-shadow: 0 8px 24px rgba(139, 111, 71, 0.3);
  z-index: 1000;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #8b6f47;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading-text {
  color: #4a3c2a;
  font-size: 14px;
  font-weight: 600;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .page-header {
    padding: 10px 12px;
  }
  
  .page-title {
    font-size: 18px;
  }
  
  .sect-container {
    padding: 10px 12px 12px;
    gap: 10px;
  }
  
  .info-card {
    padding: 12px;
  }
  
  .card-title {
    font-size: 15px;
  }
  
  .info-grid {
    gap: 10px;
  }
  
  .info-item {
    padding: 8px;
  }
  
  .info-value {
    font-size: 14px;
  }
  
  .description-content {
    font-size: 13px;
    padding: 10px;
  }
  
  .functions-grid {
    gap: 10px;
  }
  
  .function-item {
    padding: 12px 8px;
  }
  
  .function-icon {
    font-size: 24px;
  }
  
  .function-name {
    font-size: 12px;
  }
  
  .refresh-btn {
    font-size: 13px;
    padding: 10px;
  }
}
</style>
